package com.jasonm.rssminer.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.jasonm.rssminer.dao.RssFeedDAO;
import com.jasonm.rssminer.dao.SearchDAO;
import com.jasonm.rssminer.dao.SolrDAO;
import com.jasonm.rssminer.dao.UserDAO;
import com.jasonm.rssminer.dto.FeedDTO;
import com.jasonm.rssminer.dto.SearchDTO;
import com.jasonm.rssminer.dto.UserDTO;

public class RssService {

	@Autowired
	@Qualifier("rssFeedDAO")
	private RssFeedDAO rssFeedDAO;

	@Autowired
	@Qualifier("solrDAO")
	private SolrDAO solrDAO;

	@Autowired
	@Qualifier("userDAO")
	private UserDAO userDAO;

	@Autowired
	@Qualifier("searchDAO")
	private SearchDAO searchDAO;

	private static final Logger log = Logger.getLogger(RssService.class);

	public void loadRssFeeds() {

		List<FeedDTO> feeds = rssFeedDAO
				.getFeedContents("http://content.dealnews.com/dealnews/rss/popular.xml");
		for (FeedDTO feedDTO : feeds) {
			log.info(feedDTO.toString());
		}

		solrDAO.addFeedToSolr(feeds);

	}

	public void cleanSolr() {
		solrDAO.deleteAll();
	}

	public List<FeedDTO> getSearchResults(String search) {
		return solrDAO.queryFeeds(search);
	}

	public UserDTO getUserId(String email) {
		return userDAO.getUser(email);
	}

	public List<SearchDTO> getUserSearches(Integer userId) {
		return searchDAO.getSearches(userId);
	}
	
	public List<UserDTO> getAllUsers(){
		return userDAO.getUsers();
	}

}
